package com.newly.center.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newly.center.business.service.StatisticsService;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.business.Dto.StatisticsDto;
import com.newly.common.base.entity.business.Po.CheckRecord;
import com.newly.common.base.entity.business.Po.Site;
import com.newly.common.base.entity.business.Po.StatisticsDay;
import com.newly.common.base.entity.sys.dict.po.DictPo;
import com.newly.common.base.entity.sys.po.AreaPo;
import com.newly.common.mapper.business.CheckRecordMapper;
import com.newly.common.mapper.business.SiteMapper;
import com.newly.common.mapper.business.StatisticsDayMapper;
import com.newly.common.mapper.sys.AreaMapper;
import com.newly.common.mapper.sys.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @anthor ljq
 * @date 2021/11/17 14:25
 */
@Service("statisticsServiceImpl")
public class StatisticsServiceImpl extends ServiceImpl<CheckRecordMapper, CheckRecord> implements StatisticsService {

    @Autowired
    private CheckRecordMapper checkRecordMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    SiteMapper siteMapper;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    StatisticsDayMapper statisticsDayMapper;

    public R getSiteData(ICurrentTenant currentTenant, StatisticsDto statisticsDto) {
        List<String> siteIds = currentTenant.getEntityIds(AuthConstant.AuthEntityType.SITE);
        Map<String, Object> result = new HashMap<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = statisticsDto.getStartTime();
        String endTime = statisticsDto.getEndTime();
        String site = statisticsDto.getSite();

        if (startTime == null || endTime == null) {
            LocalDate now = LocalDate.now();
            endTime = df.format(now) + " 23:59:59";
            startTime = df.format(now.minusDays(3)) + " 00:00:00";
        } else {
            endTime = endTime + " 23:59:59";
            startTime = startTime + " 00:00:00";
        }
//        查询跨度超过72小时
//        if(Duration.between(LocalDateTime.parse(startTime, df2),LocalDateTime.parse(endTime, df2)).toHours()>24*3){
//            return getDataByStatisticsDay("siteName",startTime,endTime,site,siteIds);
//        }

        List<CheckRecord> checkRecords = this.checkRecordMapper.selectList(
                new QueryWrapper<CheckRecord>()
                        .lambda()
                        .between(CheckRecord::getCreateTime, startTime, endTime)
                        .eq(site != null && !StringUtils.isEmpty(site), CheckRecord::getSiteId, site)
//                        .in(CheckRecord::getSiteId,siteIds)
        );
        if (checkRecords.size() == 0) {
            result.put("totalOverloadCount", 0);
            result.put("totalNormalCount", 0);
            result.put("totalVehicleCount", 0);
            result.put("dataList", new JSONArray());
            return R.ok(result);
        }
        Map<Integer, List<CheckRecord>> collect = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getAxleNum));
        checkRecords.clear();
        List<DictPo> dictPos = dictMapper.selectList(new QueryWrapper<DictPo>()
                .lambda()
                .eq(DictPo::getType, "1"));
        //根据标重参数临时修改超重量与超重率
        for (int axleNum : collect.keySet()) {
            //读取各轴数标重
            String settingCode = (10014 + axleNum) + "";
            List<DictPo> dictList = dictPos.stream().filter(x -> settingCode.equals(x.getCode())).collect(Collectors.toList());
            if (dictList.size() == 0) {
                return R.fail("缺少标重参数");
            }
            int standardValue = Integer.parseInt(dictList.get(0).getValue());
            //设置超出重量
            collect.get(axleNum).stream().forEach(x -> x.setOverWeight(x.getTotalWeight() - standardValue > 0 ? x.getTotalWeight() - standardValue : 0));
            //设置超重率
            collect.get(axleNum).stream().forEach(x -> x.setOverRate(x.getOverWeight() > 0 ? x.getTotalWeight() / standardValue : 0));
            checkRecords.addAll(collect.get(axleNum));
        }

        List<CheckRecord> overloadRecords = checkRecords.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList());
        //总共的检测数，超限数，未超限数
        int totalOverloadCount = overloadRecords.size();
        int totalNormalCount = checkRecords.size() - totalOverloadCount;
        int totalVehicleCount = checkRecords.size();

        List<Long> siteIdList = checkRecords.stream().map(x -> x.getSiteId()).collect(Collectors.toList());
        List<Site> tmpSiteList = this.siteMapper.selectList(new QueryWrapper<Site>().lambda().eq(Site::getIsDeleted, 0).in(Site::getId, siteIdList));
        //设置站点名称
        checkRecords = checkRecords.stream().map(p -> tmpSiteList.stream().filter(m ->
                Objects.equals(p.getSiteId(), m.getId())).findFirst()
                .map(m -> {
                    p.setSiteName(m.getName());
                    return p;
                }).orElse(null))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        Map<String, List<CheckRecord>> siteData = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getSiteName));
        List<Map<String, Object>> siteList = new ArrayList<>();
        for (String siteName : siteData.keySet()) {
            List<CheckRecord> childSiteList = siteData.get(siteName);
            Map<String, Object> childSite = new HashMap<>();
            //检测数
            childSite.put("checkCount", childSiteList.size());
            //超限数
            int overloadCount = childSiteList.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList()).size();
            childSite.put("overloadCount", overloadCount);
            //未超限数
            childSite.put("normalCount", childSiteList.size() - overloadCount);
            //超限比
            childSite.put("overloadRate", (double) overloadCount / (double) childSiteList.size());
            //平均超限幅度
            double averageOverRate = childSiteList.stream().map(CheckRecord::getOverRate).reduce(Double::sum).get() / (double) childSiteList.size();
            childSite.put("averageOverRate", averageOverRate);
            childSite.put("siteName", siteName);
            siteList.add(childSite);
        }
        result.put("totalOverloadCount", totalOverloadCount);
        result.put("totalNormalCount", totalNormalCount);
        result.put("totalVehicleCount", totalVehicleCount);
        result.put("dataList", siteList);
        return R.ok(result);
    }

    public R getTimeData(ICurrentTenant currentTenant, StatisticsDto statisticsDto) {
        List<String> siteIds = currentTenant.getEntityIds(AuthConstant.AuthEntityType.SITE);
        Map<String, Object> result = new HashMap<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = statisticsDto.getStartTime();
        String endTimeStr = statisticsDto.getEndTime();
        String site = statisticsDto.getSite();

        if (startTimeStr == null || endTimeStr == null) {
            LocalDate now = LocalDate.now();
            endTimeStr = df2.format(now) + " 00:00:00";
            startTimeStr = df2.format(now.minusDays(1)) + " 00:00:00";
        }
        LocalDateTime startTime = LocalDateTime.parse(startTimeStr, df);
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr, df);
        if (Duration.between(startTime, endTime).toHours() > 24 * 3) {
            return R.fail("时间数据的查询跨度为72小时");
        }

        List<CheckRecord> checkRecords = this.checkRecordMapper.selectList(
                new QueryWrapper<CheckRecord>()
                        .lambda()
                        .between(CheckRecord::getCreateTime, startTime, endTime)
                        .eq(site != null && !StringUtils.isEmpty(site), CheckRecord::getSiteId, site)
//                        .in(CheckRecord::getSiteId,siteIds)
        );
        if (checkRecords.size() == 0) {
            result.put("totalOverloadCount", 0);
            result.put("totalNormalCount", 0);
            result.put("totalVehicleCount", 0);
            result.put("dataList", new JSONArray());
            return R.ok(result);
        }
        Map<Integer, List<CheckRecord>> collect = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getAxleNum));
        checkRecords.clear();
        List<DictPo> dictPos = dictMapper.selectList(new QueryWrapper<DictPo>()
                .lambda()
                .eq(DictPo::getType, "1"));
        //根据标重参数临时修改超重量与超重率
        for (int axleNum : collect.keySet()) {
            //读取各轴数标重
            String settingCode = (10014 + axleNum) + "";
            List<DictPo> dictList = dictPos.stream().filter(x -> settingCode.equals(x.getCode())).collect(Collectors.toList());
            if (dictList.size() == 0) {
                return R.fail("缺少标重参数");
            }
            int standardValue = Integer.parseInt(dictList.get(0).getValue());
            //设置超出重量
            collect.get(axleNum).stream().forEach(x -> x.setOverWeight(x.getTotalWeight() - standardValue > 0 ? x.getTotalWeight() - standardValue : 0));
            //设置超重率
            collect.get(axleNum).stream().forEach(x -> x.setOverRate(x.getOverWeight() > 0 ? x.getTotalWeight() / standardValue : 0));
            checkRecords.addAll(collect.get(axleNum));
        }

        List<CheckRecord> overloadRecords = checkRecords.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList());
        //总共的检测数，超限数，未超限数
        int totalOverloadCount = overloadRecords.size();
        int totalNormalCount = checkRecords.size() - totalOverloadCount;
        int totalVehicleCount = checkRecords.size();

        Map<String, List<CheckRecord>> timeData = checkRecords.stream().collect(Collectors.groupingBy(item -> item.getCreateTime().substring(0, 13)));
        List<Map<String, Object>> siteList = new ArrayList<>();
        for (String time : timeData.keySet()) {
            List<CheckRecord> childSiteList = timeData.get(time);
            Map<String, Object> childSite = new HashMap<>();
            //检测数
            childSite.put("checkCount", childSiteList.size());
            //超限数
            int overloadCount = childSiteList.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList()).size();
            childSite.put("overloadCount", overloadCount);
            //未超限数
            childSite.put("normalCount", childSiteList.size() - overloadCount);
            //超限比
            childSite.put("overloadRate", (double) overloadCount / (double) childSiteList.size());
            //平均超限幅度
            double averageOverRate = childSiteList.stream().map(CheckRecord::getOverRate).reduce(Double::sum).get() / (double) childSiteList.size();
            childSite.put("averageOverRate", averageOverRate);
            childSite.put("time", time + ":00:00");
            siteList.add(childSite);
        }
        siteList.sort(Comparator.comparing(s -> s.get("time").toString()));

        result.put("totalOverloadCount", totalOverloadCount);
        result.put("totalNormalCount", totalNormalCount);
        result.put("totalVehicleCount", totalVehicleCount);
        result.put("dataList", siteList);
        return R.ok(result);
    }

    public R getAreaData(ICurrentTenant currentTenant, StatisticsDto statisticsDto) {
        List<String> siteIds = currentTenant.getEntityIds(AuthConstant.AuthEntityType.SITE);
        Map<String, Object> result = new HashMap<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = statisticsDto.getStartTime();
        String endTime = statisticsDto.getEndTime();
        String site = statisticsDto.getSite();

        if (startTime == null || endTime == null) {
            LocalDate now = LocalDate.now();
            endTime = df.format(now) + " 23:59:59";
            startTime = df.format(now.minusDays(3)) + " 00:00:00";
        } else {
            endTime = endTime + " 23:59:59";
            startTime = startTime + " 00:00:00";
        }
        //查询跨度超过72小时
//        if(Duration.between(LocalDateTime.parse(startTime, df2),LocalDateTime.parse(endTime, df2)).toHours()>24*3){
//            return getDataByStatisticsDay("areaName",startTime,endTime,site,siteIds);
//        }

        List<CheckRecord> checkRecords = this.checkRecordMapper.selectList(
                new QueryWrapper<CheckRecord>()
                        .lambda()
                        .between(CheckRecord::getCreateTime, startTime, endTime)
                        .eq(site != null && !StringUtils.isEmpty(site), CheckRecord::getSiteId, site)
//                        .in(CheckRecord::getSiteId,siteIds)
        );
        if (checkRecords.size() == 0) {
            result.put("totalOverloadCount", 0);
            result.put("totalNormalCount", 0);
            result.put("totalVehicleCount", 0);
            result.put("dataList", new JSONArray());
            return R.ok(result);
        }
        Map<Integer, List<CheckRecord>> collect = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getAxleNum));
        checkRecords.clear();
        List<DictPo> dictPos = dictMapper.selectList(new QueryWrapper<DictPo>()
                .lambda()
                .eq(DictPo::getType, "1"));
        //根据标重参数临时修改超重量与超重率
        for (int axleNum : collect.keySet()) {
            //读取各轴数标重
            String settingCode = (10014 + axleNum) + "";
            List<DictPo> dictList = dictPos.stream().filter(x -> settingCode.equals(x.getCode())).collect(Collectors.toList());
            if (dictList.size() == 0) {
                return R.fail("缺少标重参数");
            }
            int standardValue = Integer.parseInt(dictList.get(0).getValue());
            //设置超出重量
            collect.get(axleNum).stream().forEach(x -> x.setOverWeight(x.getTotalWeight() - standardValue > 0 ? x.getTotalWeight() - standardValue : 0));
            //设置超重率
            collect.get(axleNum).stream().forEach(x -> x.setOverRate(x.getOverWeight() > 0 ? x.getTotalWeight() / standardValue : 0));
            checkRecords.addAll(collect.get(axleNum));
        }

        List<CheckRecord> overloadRecords = checkRecords.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList());
        //总共的检测数，超限数，未超限数
        int totalOverloadCount = overloadRecords.size();
        int totalNormalCount = checkRecords.size() - totalOverloadCount;
        int totalVehicleCount = checkRecords.size();

        List<Long> siteIdList = checkRecords.stream().map(x -> x.getSiteId()).collect(Collectors.toList());
        List<Site> tmpSiteList = this.siteMapper.selectList(new QueryWrapper<Site>().lambda().eq(Site::getIsDeleted, 0).in(Site::getId, siteIdList));
        List<String> areaCodeList = tmpSiteList.stream().map(x -> x.getAreaCode()).collect(Collectors.toList());
        if (!ObjectUtil.isEmpty(areaCodeList)) {
            List<AreaPo> areaPos = this.areaMapper.selectList(new QueryWrapper<AreaPo>().lambda().in(AreaPo::getAreaCode, areaCodeList));
            //设置站点区域名称
            List<Site> tmpSiteList2 = tmpSiteList.stream().map(p -> areaPos.stream().filter(m ->
                    Objects.equals(p.getAreaCode(), m.getAreaCode())).findFirst()
                    .map(m -> {
                        p.setAreaCode(m.getAreaName());
                        return p;
                    }).orElse(null))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            //设置检测站点区域名称
            checkRecords = checkRecords.stream().map(p -> tmpSiteList2.stream().filter(m ->
                    Objects.equals(p.getSiteId(), m.getId())).findFirst()
                    .map(m -> {
                        p.setSiteName(m.getAreaCode());
                        return p;
                    }).orElse(null))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
        }

        Map<String, List<CheckRecord>> areaData = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getSiteName));
        List<Map<String, Object>> siteList = new ArrayList<>();
        for (String area : areaData.keySet()) {
            List<CheckRecord> childSiteList = areaData.get(area);
            Map<String, Object> childSite = new HashMap<>();
            //检测数
            childSite.put("checkCount", childSiteList.size());
            //超限数
            int overloadCount = childSiteList.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList()).size();
            childSite.put("overloadCount", overloadCount);
            //未超限数
            childSite.put("normalCount", childSiteList.size() - overloadCount);
            //超限比
            childSite.put("overloadRate", (double) overloadCount / (double) childSiteList.size());
            //平均超限幅度
            double averageOverRate = childSiteList.stream().map(CheckRecord::getOverRate).reduce(Double::sum).get() / (double) childSiteList.size();
            childSite.put("averageOverRate", averageOverRate);
            childSite.put("areaName", area);
            siteList.add(childSite);
        }
        result.put("totalOverloadCount", totalOverloadCount);
        result.put("totalNormalCount", totalNormalCount);
        result.put("totalVehicleCount", totalVehicleCount);
        result.put("dataList", siteList);
        return R.ok(result);
    }

    public R getAxleData(ICurrentTenant currentTenant, StatisticsDto statisticsDto) {
        List<String> siteIds = currentTenant.getEntityIds(AuthConstant.AuthEntityType.SITE);
        Map<String, Object> result = new HashMap<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = statisticsDto.getStartTime();
        String endTime = statisticsDto.getEndTime();
        String site = statisticsDto.getSite();

        if (startTime == null || endTime == null) {
            LocalDate now = LocalDate.now();
            endTime = df.format(now) + " 23:59:59";
            startTime = df.format(now.minusDays(3)) + " 00:00:00";
        } else {
            endTime = endTime + " 23:59:59";
            startTime = startTime + " 00:00:00";
        }
        //查询跨度超过72小时
//        if(Duration.between(LocalDateTime.parse(startTime, df2),LocalDateTime.parse(endTime, df2)).toHours()>24*3){
//            return getDataByStatisticsDay("axleNum",startTime,endTime,site,siteIds);
//        }

        List<CheckRecord> checkRecords = this.checkRecordMapper.selectList(
                new QueryWrapper<CheckRecord>()
                        .lambda()
                        .between(CheckRecord::getCreateTime, startTime, endTime)
                        .eq(site != null && !StringUtils.isEmpty(site), CheckRecord::getSiteId, site)
//                        .in(CheckRecord::getSiteId,siteIds)
        );
        if (checkRecords.size() == 0) {
            result.put("totalOverloadCount", 0);
            result.put("totalNormalCount", 0);
            result.put("totalVehicleCount", 0);
            result.put("dataList", new JSONArray());
            return R.ok(result);
        }
        Map<Integer, List<CheckRecord>> collect = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getAxleNum));
        checkRecords.clear();
        List<DictPo> dictPos = dictMapper.selectList(new QueryWrapper<DictPo>()
                .lambda()
                .eq(DictPo::getType, "1"));
        //根据标重参数临时修改超重量与超重率
        for (int axleNum : collect.keySet()) {
            //读取各轴数标重
            String settingCode = (10014 + axleNum) + "";
            List<DictPo> dictList = dictPos.stream().filter(x -> settingCode.equals(x.getCode())).collect(Collectors.toList());
            if (dictList.size() == 0) {
                return R.fail("缺少标重参数");
            }
            int standardValue = Integer.parseInt(dictList.get(0).getValue());
            //设置超出重量
            collect.get(axleNum).stream().forEach(x -> x.setOverWeight(x.getTotalWeight() - standardValue > 0 ? x.getTotalWeight() - standardValue : 0));
            //设置超重率
            collect.get(axleNum).stream().forEach(x -> x.setOverRate(x.getOverWeight() > 0 ? x.getTotalWeight() / standardValue : 0));
            checkRecords.addAll(collect.get(axleNum));
        }

        List<CheckRecord> overloadRecords = checkRecords.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList());
        //总共的检测数，超限数，未超限数
        int totalOverloadCount = overloadRecords.size();
        int totalNormalCount = checkRecords.size() - totalOverloadCount;
        int totalVehicleCount = checkRecords.size();

        Map<Integer, List<CheckRecord>> axleData = checkRecords.stream().collect(Collectors.groupingBy(CheckRecord::getAxleNum));
        List<Map<String, Object>> siteList = new ArrayList<>();
        for (Integer axleNum : axleData.keySet()) {
            List<CheckRecord> childSiteList = axleData.get(axleNum);
            Map<String, Object> childSite = new HashMap<>();
            //检测数
            childSite.put("checkCount", childSiteList.size());
            //超限数
            int overloadCount = childSiteList.stream().filter(x -> x.getOverWeight() > 0).collect(Collectors.toList()).size();
            childSite.put("overloadCount", overloadCount);
            //未超限数
            childSite.put("normalCount", childSiteList.size() - overloadCount);
            //超限比
            childSite.put("overloadRate", (double) overloadCount / (double) childSiteList.size());
            //平均超限幅度
            double averageOverRate = childSiteList.stream().map(CheckRecord::getOverRate).reduce(Double::sum).get() / (double) childSiteList.size();
            childSite.put("averageOverRate", averageOverRate);
            childSite.put("axleNum", axleNum);
            siteList.add(childSite);
        }
        result.put("totalOverloadCount", totalOverloadCount);
        result.put("totalNormalCount", totalNormalCount);
        result.put("totalVehicleCount", totalVehicleCount);
        result.put("dataList", siteList);
        return R.ok(result);
    }

    //从天级统计表查询数据，避免从记录表查询过多数据进行计算超重率等
    private R getDataByStatisticsDay(String key, String startTime, String endTime, String site, List<Long> siteIds) {
        Map<String, Object> result = new HashMap<>();
        if (site != null && !StringUtils.isEmpty(site)) {
            Site siteEntity = siteMapper.selectOne(new QueryWrapper<Site>().lambda().eq(Site::getId, site));
            if (siteEntity != null) {
                site = siteEntity.getName();
            }
        }
        List<Site> sites = siteMapper.selectList(new QueryWrapper<Site>().lambda().in(Site::getId, siteIds));
        List<String> siteNameList = sites.stream().map(x -> x.getName()).collect(Collectors.toList());
        List<StatisticsDay> statisticsDays = statisticsDayMapper.selectList(new QueryWrapper<StatisticsDay>().lambda()
                .between(StatisticsDay::getCreateTime, startTime, endTime)
                .eq(site != null && !StringUtils.isEmpty(site), StatisticsDay::getSiteName, site)
                .in(StatisticsDay::getSiteName, siteNameList));
        //统计总共的检测数，超限数，未超限数
        int totalOverloadCount = 0;
        int totalNormalCount = 0;
        int totalVehicleCount = 0;
        for (StatisticsDay item : statisticsDays) {
            totalOverloadCount += item.getCheckNum();
            totalNormalCount += item.getNormalNum();
            totalVehicleCount += item.getOverweightNum();
        }
        Map<Object, List<StatisticsDay>> collect = new HashMap<>();
        switch (key) {
            case "siteName":
                collect = statisticsDays.stream().collect(Collectors.groupingBy(StatisticsDay::getSiteName));
                break;
            case "areaName":
                List<Site> tmpSiteList = this.siteMapper.selectList(new QueryWrapper<Site>().lambda().eq(Site::getIsDeleted, 0).in(Site::getName, siteNameList));
                List<String> areaCodeList = tmpSiteList.stream().map(x -> x.getAreaCode()).collect(Collectors.toList());
                List<AreaPo> areaPos = this.areaMapper.selectList(new QueryWrapper<AreaPo>().lambda().in(AreaPo::getAreaCode, areaCodeList));

                //设置站点区域名称
                List<Site> tmpSiteList2 = tmpSiteList.stream().map(p -> areaPos.stream().filter(m ->
                        Objects.equals(p.getAreaCode(), m.getAreaCode())).findFirst()
                        .map(m -> {
                            p.setAreaCode(m.getAreaName());
                            return p;
                        }).orElse(null))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

                //设置检测站点区域名称
                statisticsDays = statisticsDays.stream().map(p -> tmpSiteList2.stream().filter(m ->
                        Objects.equals(p.getSiteName(), m.getName())).findFirst()
                        .map(m -> {
                            p.setSiteName(m.getAreaCode());
                            return p;
                        }).orElse(null))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());
                collect = statisticsDays.stream().collect(Collectors.groupingBy(StatisticsDay::getSiteName));
                break;
            case "axleNum":
                collect = statisticsDays.stream().collect(Collectors.groupingBy(StatisticsDay::getAxleNum));
                break;
        }
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (Object keyIndex : collect.keySet()) {
            List<StatisticsDay> statisticsDaysList = collect.get(keyIndex);
            int checkCount = 0;
            int overloadCount = 0;
            int normalCount = 0;
            double overloadRate = 0;
            double averageOverRate = 0;
            for (StatisticsDay item : statisticsDaysList) {
                checkCount += item.getCheckNum();
                normalCount += item.getNormalNum();
                overloadCount += item.getOverweightNum();
                averageOverRate += item.getRate();
            }
            overloadRate = overloadCount / checkCount;
            averageOverRate = averageOverRate / checkCount;
            Map<String, Object> childSite = new HashMap<>();
            //检测数
            childSite.put("checkCount", checkCount);
            //超限数
            childSite.put("overloadCount", overloadCount);
            //未超限数
            childSite.put("normalCount", normalCount);
            //超限比
            childSite.put("overloadRate", overloadRate);
            //平均超限幅度
            childSite.put("averageOverRate", averageOverRate);
            childSite.put(key, keyIndex);
            dataList.add(childSite);
        }
        result.put("totalOverloadCount", totalOverloadCount);
        result.put("totalNormalCount", totalNormalCount);
        result.put("totalVehicleCount", totalVehicleCount);
        result.put("dataList", dataList);
        return R.ok(result);
    }

}
